package com.hilihili.controller;

import com.hilihili.service.IStaticResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 静态资源后端接口
 *
 * @author machi
 * @date 2023/12/02
 */
@CrossOrigin(origins = "http://localhost:8080")
@Slf4j
@RestController
@RequestMapping("/poll")
public class StaticSourceController {

    @Autowired
    IStaticResourceService staticResourceService;

    /**
     * @param fileName 文件名称 + 后缀名
     *                 /105/1739239750688165888/index.m3u8
     * @param response 响应
     */
    @GetMapping("/getVideoStream/{prefix}/{videoId}/{fileName}")
    public void getVideo(@PathVariable("prefix") String prefix,@PathVariable("fileName") String fileName, @PathVariable("videoId") String videoId, HttpServletResponse response) {
        String path = String.join("/",prefix, videoId, fileName);
        staticResourceService.getVideo(path, response);

    }

    @GetMapping("/getVideoStream/{prefix}/{videoId}/{ts}/{fileName}")
    public void getTsVideo(@PathVariable("prefix") String prefix,@PathVariable("ts") String ts, @PathVariable("fileName") String fileName, @PathVariable("videoId") String videoId, HttpServletResponse response) {
        String path = String.join("/",prefix, videoId, ts, fileName);
        staticResourceService.getVideo(path, response);

    }

    @GetMapping()
    public void getImage(String imageUrl, HttpServletResponse response) {
        staticResourceService.getImage(imageUrl, response);
    }

    @GetMapping("/imag/{prefix}/{videoId}/{fileName}")
    public void getLocalImage(@PathVariable("prefix") String prefix,@PathVariable("fileName") String fileName, @PathVariable("videoId") String videoId, HttpServletResponse response) {
        String path = String.join("/", prefix, videoId, fileName);
        staticResourceService.getImage(path, response);
    }

}
